# 密码修改
import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from Common.Utils import ChatDriver
from selenium.webdriver.support import expected_conditions as EC

# 登录状态下
class ForumPassword:
    url = ""
    driver = ""

    def __init__(self):
        self.driver = ChatDriver.driver

    def clear_input_fields(self):
        self.driver.find_element(By.CSS_SELECTOR, "#settings_input_oldPassword").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#settings_input_newPassword").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#settings_input_passwordRepeat").clear()

    def fill_password_and_submit(self, oldpassword, newpassword, passwordrepeat):
        self.driver.find_element(By.CSS_SELECTOR, "#settings_input_oldPassword").send_keys(oldpassword)
        self.driver.find_element(By.CSS_SELECTOR, "#settings_input_newPassword").send_keys(newpassword)
        self.driver.find_element(By.CSS_SELECTOR, "#settings_input_passwordRepeat").send_keys(passwordrepeat)
        # 定位提交按钮元素
        submit_button = self.driver.find_element(By.CSS_SELECTOR, "#settings_submit_password")
        # 执行 JavaScript 代码将元素滚动到可见区域
        self.driver.execute_script("arguments[0].scrollIntoView(true);", submit_button)
        # 等待一段时间，确保页面滚动完成
        time.sleep(1)
        # 点击按钮
        submit_button.click()
        # 修改失败依旧在用户中心
        self.driver.find_element(By.CSS_SELECTOR, "#bit-forum-content > div.page-header.d-print-none > div > div > div > h2")
        time.sleep(0.5)
        # 添加屏幕截图
        ChatDriver.getScreenShort()
        # ChatDriver.get_alert_image()

    # 修改密码成功
    def ForumSucPassword(self):
        # 清空原密码输入框、新密码输入框、确认密码输入框
        self.clear_input_fields()
        # 输入正确的原密码、新密码、确认密码
        self.driver.find_element(By.CSS_SELECTOR, "#settings_input_oldPassword").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#settings_input_newPassword").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR, "#settings_input_passwordRepeat").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR, "#settings_submit_password").click()  # 点击提交修改
        WebDriverWait(self.driver, 5).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "body > div > div > div > div:nth-child(1) > div > div.card.card-md > div > h2")))


    # 修改密码失败
    def ForumFailPassword(self):
        self.driver.find_element(By.CSS_SELECTOR, "#index_nav_avatar").click()
        self.driver.find_element(By.CSS_SELECTOR, "#index_user_settings").click()
        test_case = [
            {"oldpassword": "123", "newpassword": "1234", "passwordrepeat": ""},
            {"oldpassword": "123", "newpassword": "", "passwordrepeat": "1234"},
            {"oldpassword": "11", "newpassword": "1234", "passwordrepeat": "1234"},
            {"oldpassword": "1", "newpassword": "", "passwordrepeat": "1234"},
            {"oldpassword": "1", "newpassword": "1234", "passwordrepeat": ""},
            {"oldpassword": "123", "newpassword": "", "passwordrepeat": ""},
            {"oldpassword": "", "newpassword": "", "passwordrepeat": ""}
        ]
        # 遍历执行所有测试用例
        for case in test_case:
            # 清空输入框
            self.clear_input_fields()
            # 填写表单并提交
            self.fill_password_and_submit(
                oldpassword=case["oldpassword"],
                newpassword=case["newpassword"],
                passwordrepeat=case["passwordrepeat"],

            )
